package com.lingshi.shopping_manager_api.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import com.lingshi.shopping.entity.Admin;
import com.lingshi.shopping.result.BaseResult;
import com.lingshi.shopping.service.AdminService;
import com.lingshi.shopping_manager_api.service.LoginService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员管理
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @DubboReference
    private AdminService adminService;


    @Autowired
    private LoginService loginService;

    /**
     * 新增管理员
     *
     * @param admin 管理员对象
     * @return
     */
    @PostMapping("/add")
//    @SaCheckPermission("/admin/add")
    public BaseResult add(@RequestBody Admin admin) {
        String md5 = SaSecureUtil.md5(admin.getPassword());
        admin.setPassword(md5);
        adminService.add(admin);
        return BaseResult.success();
    }

    /**
     * 修改管理员
     *
     * @param admin 管理员对象
     * @return
     */
    @PutMapping("/update")
//    @SaCheckPermission("/admin/update")
    public BaseResult update(@RequestBody Admin admin) {
        String md5 = SaSecureUtil.md5(admin.getPassword());
        admin.setPassword(md5);
        adminService.update(admin);
        return BaseResult.success();
    }

    /**
     * 删除管理员
     *
     * @param aid 管理员id
     * @return
     */
    @DeleteMapping("/delete")
//    @SaCheckPermission("/admin/delete")
    public BaseResult delete(Long aid) {
        adminService.delete(aid);
        return BaseResult.success();
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页条数
     * @return 管理员员分页对象
     */
    @GetMapping("/search")
    @SaCheckPermission("/admin/search")
    public BaseResult search(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size) {
        return BaseResult.success(adminService.search(page, size));
    }

    /**
     * 根据id查询管理员
     *
     * @param aid 管理员id
     * @return 管理员员对象
     */
    @GetMapping("/findById")
    public BaseResult findById(Long aid) {
        return BaseResult.success(adminService.findById(aid));
    }

    /**
     * 管理员分配（修改）角色
     *
     * @param aid  管理员id
     * @param rids 角色id，数组
     * @return
     */
    @PutMapping("/updateRoleToAdmin")
//    @SaCheckPermission("/admin/updateRoleToAdmin")
    public BaseResult updateRoleToAdmin(Long aid, Long[] rids) {
        adminService.updateRoleToAdmin(aid, rids);
        return BaseResult.success();
    }

    /**
     * 登录
     * @param username 账号
     * @param password 密码
     * @return
     */
    @PostMapping("/login")
    public BaseResult login(String username, String password) {
        return loginService.login(username, password);
    }

    @GetMapping("/getUsername")
    public BaseResult getUsername() {
        return BaseResult.success(StpUtil.getLoginIdAsString());
    }

    @PostMapping("/logout")
    public BaseResult logout() {
        StpUtil.logout();
        return BaseResult.success();
    }
}
